﻿

/********************************************
Module Name:  Global Javascript Code
Created By:   Justin Jiang
Created Date: 2008-06-25
Description:
1. Add Popup function used for Popup a 
fixed window
2. Add SynScroll function used for Synchro-
nize different DIV scrolling
---------------------------------------------
Modified By:   Justin Jiang
Modified Date: 2007-07-03
Description:
1. Add AutoFill function used for Auto Fill
Related TextBox
---------------------------------------------
Modified By:   Justin Jiang
Modified Date: 2008-09-10
Description:
1. Add Selection Manager
---------------------------------------------
Modified By:   Ma Qian
Modified Date: 2008-10-22
Description:
1. Add Upload File
---------------------------------------------
Modified By:    Justin Jiang
Modified Date: 2008-11-29
Description:
1. PanelBarStateManager
2. OpenDownload
*********************************************/

// Popup Window
function Popup(url, formKey, width, height) {
    formKey = formKey.replace(/\s*/g, "");
    var l = (screen.width - width) / 2;
    var t = (screen.height - height) / 2;
    var s = "width=" + width + "px, height=" + height + "px, top=" + t + "px, left=" + l + "px";
    window.open(url, formKey, s + ",menubar=false,toolbar=false, statusbar=false, scrollbars=false");
}

// Open Download
function AsynDownLoad(url, frmId) {
    var frmDownLoad = document.getElementById(frmId);
    frmDownLoad.src = url;
}
function OpenDownload(url, frmId) {
    var frmDownLoad = document.getElementById(frmId);
    if (frmDownLoad != undefined) {
        window.setTimeout("AsynDownLoad('" + url + "','" + frmId + "')", 500);
    }
    else {
        Popup(url, "DownLoad", 450, 200);
    }
}

// Resize window size
function InitWindowSize() {
    self.moveTo(0, 0);
    self.resizeTo(screen.availWidth, screen.availHeight);
}

// Show Dialog
function ShowDialog(url, title, width, height) {
    if (parent != null) {
        // Force reload in order to guarantee that the
        // onload event handler of the dialog which configures
        // it executes on every show.
        var oWnd = parent.window.open(url, title);
        oWnd.setSize(width, height);
        oWnd.center();
    }
    else {
        Popup(url, title, width, height);
    }
}

// Close Dialog
function CloseDialog() {
    if (parent != null) {
        var oManager = parent.GetRadWindowManager();
        var oWnd = oManager.getActiveWindow();
        oWnd.close();
    }
    else {
        close();
    }
}

// Synchronous Scroll
function SynScroll(srcId, hTargetId, vTargetId, tableId) {
    // Get control instance
    var srcCtrl = document.getElementById(srcId);
    var hTargetCtrl = document.getElementById(hTargetId);
    var vTargetCtrl = document.getElementById(vTargetId);
    var table = document.getElementById(tableId);

    if (srcCtrl == undefined) {
        return;
    }
    var originalHWidth = srcCtrl.clientWidth;
    var originalVHeight = srcCtrl.clientHeight;

    // Set sub table width
    if (table != undefined) {
        table.style.width = originalHWidth;
    }

    // Horizontal scroll
    var hScrollSpan = srcCtrl.scrollLeft;
    if (hTargetCtrl != undefined) {
        hTargetCtrl.style.marginLeft = -hScrollSpan;
        hTargetCtrl.style.width = originalHWidth + hScrollSpan;
    }

    // Vertical scroll
    var vScrollSpan = srcCtrl.scrollTop;
    if (vTargetCtrl != undefined) {
        vTargetCtrl.style.marginTop = -vScrollSpan;
        vTargetCtrl.style.height = originalVHeight + vScrollSpan;
    }
}

// Auto Fill Related TextBox with current TextBox value
function AutoFill(srcId, targetIds) {
    var txtSrc = document.getElementById(srcId);
    var Ids = targetIds.split(",");
    for (i = 0; i < Ids.length; i++) {
        var txtTarget = document.getElementById(Ids[i]);
        txtTarget.value = txtSrc.value;
    }
}

function SelectionManager(hiddenControlId) {
    this.HiddenControlID = hiddenControlId;
}

SelectionManager.prototype.OnSelect = function Select(value) {
    var hiddenControl = document.getElementById(this.HiddenControlID);
    var hiddenValue = hiddenControl.value;
    if (hiddenValue.indexOf(value) < 0) {
        hiddenValue = hiddenValue + "," + value;
    }
    hiddenControl.value = hiddenValue;
}

SelectionManager.prototype.OnUnselect = function Unselect(value) {
    var hiddenControl = document.getElementById(this.HiddenControlID);
    var hiddenValue = "";

    if (hiddenControl.value.indexOf(value) > -1) {
        var values = hiddenControl.value.split(",");
        var i = 0;
        for (i = 0; i < values.length; i++) {
            if (values[i] != value && values[i] != "") {
                hiddenValue = hiddenValue + "," + values[i];
            }
        }
    }
    hiddenControl.value = hiddenValue;
}

// upload sepcial file : *.xls 
function controlStatus(oFupID, oBtnID, msg) {
    var fup = document.getElementById(oFupID);
    var txt = fup.value;
    var btn = document.getElementById(oBtnID);
    if (txt != "") {
        var fnames = txt.split('\\');
        if (fnames.length > 1) {
            fnames = fnames[fnames.length - 1].split('.');
            if (fnames.length > 1) {
                var fileType = fnames[fnames.length - 1];
                if (fileType.toLowerCase() == "xls") {
                    btn.disabled = false;
                }
                else {
                    alert(msg);
                    btn.disabled = true;
                }
            }
        }
        else {
            btn.disabled = true;
        }
    }
    else {
        alert(msg);
        btn.disabled = true;
    }
}

// upload file
function controlStatusTwo(oFupID, oBtnID, msg) {
    var fup = document.getElementById(oFupID);
    var txt = fup.value;
    var btn = document.getElementById(oBtnID);
    if (txt != "") {
        var fnames = txt.split('\\');
        if (fnames.length > 1) {
            fnames = fnames[fnames.length - 1].split('.');
            if (fnames.length > 1) {
                btn.disabled = false;
            }
            else {
                btn.disabled = true;
                alert(msg);
            }
        }
        else {
            btn.disabled = true;
        }
    }
    else {
        btn.disabled = true;
        alert(msg);
    }
}

// Define State Manager Class
// ERROR: MULTI-EXTENSIBLE TABLE COLLAPSE WILL CAUSE STATE ERROR
function PanelBarStateManager() { }

// Self defined singleton constructor
PanelBarStateManager.instance = null;
PanelBarStateManager.getInstance = function() {
    if (PanelBarStateManager.instance == null) {
        PanelBarStateManager.instance = new PanelBarStateManager();
        PanelBarStateManager.instance._containerId = null;
    }
    return PanelBarStateManager.instance;
}

// Set the expended div ids container HiddenField
PanelBarStateManager.SetContainer = function(id) {
    // Reset container id
    PanelBarStateManager.getInstance()._containerId = id;
}

// Expend the DIV
PanelBarStateManager.Expend = function(id) {
    if (PanelBarStateManager.getInstance()._containerId == null) {
        return;
    }

    var pnl = document.getElementById(id);
    var container = document.getElementById(PanelBarStateManager.getInstance()._containerId);
    if (pnl == undefined || container == undefined) {
        return;
    }

    if (pnl.tagName.toUpperCase() != "DIV") {
        return;
    }

    pnl.style.display = "block";

    var keys = container.value.split(",");
    var i = 0;
    var isInserted = false;

    container.value = "";
    for (i = 0; i < keys.length; i++) {
        if (keys[i] == "") {
            continue;
        }
        if (keys[i].toLowerCase() == id.toLowerCase()) {
            isInserted = true;
        }
        container.value = container.value + "," + keys[i].toLowerCase();
    }
    if (!isInserted) {
        container.value = container.value + "," + id.toLowerCase();
    }
}

// Collapse DIV
PanelBarStateManager.Collapse = function(id) {
    if (PanelBarStateManager.getInstance()._containerId == null) {
        return;
    }

    var pnl = document.getElementById(id);
    var container = document.getElementById(PanelBarStateManager.getInstance()._containerId);
    if (pnl == undefined || container == undefined) {
        return;
    }

    if (pnl.tagName.toUpperCase() != "DIV") {
        return;
    }

    pnl.style.display = "none";

    var keys = container.value.split(",");
    var i = 0;

    container.value = "";
    for (i = 0; i < keys.length; i++) {
        if (keys[i] == "") {
            continue;
        }
        if (id.toLowerCase() == keys[i].toLowerCase()) {
            continue;
        }
        container.value = container.value + "," + keys[i];
    }
}

// Expend or Collapse DIV
// If current status is Expended then collapse it
// If current status is Collapsed then expend it
PanelBarStateManager.ExpendOrCollapse = function(id) {
    var div = document.getElementById(id);
    if (div == undefined) {
        return;
    }
    if (div.style.display.toUpperCase() != "BLOCK") {
        PanelBarStateManager.Expend(id);
    }
    else {
        PanelBarStateManager.Collapse(id);
    }
}

// Add by MaQian
// This function will expand divs, ids of those divs are still in container
PanelBarStateManager.ExpandLeft = function() {
    var container = document.getElementById(PanelBarStateManager.getInstance()._containerId);
    if (container == undefined || container == null)
        return;
    var keys = container.value.split(",");
    var i = 0;

    for (i = 0; i < keys.length; i++) {
        if (keys[i] == "")
            continue;

        var eObj = document.getElementById(keys[i]);
        if (eObj == undefined)
            continue;

        eObj.style.display = "block";
    }
}

// Expend all DIVs
//function PanelBarStateManager.ExpendAll() {
//    var key = "_exp_col_";
//    var divs = document.all.tags("DIV");
//    var i = 0;
//    for (i = 0; i < divs.length; i++) {
//        var div = divs[i];
//        if (div.id.indexOf(key) >= 0) {
//            PanelBarStateManager.Expend(div.id);
//        }
//    }
//}

// Collapse all DIVs
//function PanelBarStateManager.CollapseAll() {
//    var key = "_exp_col_";
//    var divs = document.all.tags("DIV");
//    var i = 0;
//    for (i = 0; i < divs.length; i++) {
//        var div = divs[i];
//        if (div.id.indexOf(key) >= 0) {
//            PanelBarStateManager.Collapse(div.id);
//        }
//    }
//}


//check All
function selectAll(bool, ctlName) {
    var ctl = document.getElementById(ctlName);
    var checkbox = ctl.getElementsByTagName('input');
    for (var i = 0; i < checkbox.length; i++) {
        if (checkbox[i].type == 'checkbox' && checkbox[i].disabled != true) {
            checkbox[i].checked = bool;
        }
    }
}

//Confirm

function myConfirm(Message_DeleteConfirm) {
    if (!confirm(Message_DeleteConfirm)) {
        return false;
    }
}

function myConfirm1(obj, Message_DeleteConfirm) {
    if (obj.disabled == true) {
        return;
    }
    if (!confirm(Message_DeleteConfirm)) {
        return false;
    }
}
